package net.gyapp.markim.utils;

import android.util.Log;

import com.easyder.wrapper.utils.LogUtils;

import org.jim.common.base.ImPacket;
import org.jim.common.packets.Command;
import org.jim.common.tcp.Protocol;

import java.nio.ByteBuffer;

/**
 * Author:  winds
 * Data:    2018/7/16
 * Version: 1.0
 * Desc:
 */


public class PacketUtils {

    public static int getBodyLength(byte[] header) {
        ByteBuffer buffer = ByteBuffer.wrap(header);
        //获取第一个字节协议版本号;
        byte version = buffer.get();
        if (version != Protocol.VERSION) {
            Log.e(" -- Socket -- ", "协议版本号不匹配");

        }
        //标志位
        byte maskByte = buffer.get();
        Integer synSeq = 0;
        if (ImPacket.decodeHasSynSeq(maskByte)) {//同步发送;
            synSeq = buffer.getInt();
        }
        //cmd命令码
        byte cmdByte = buffer.get();
        if (Command.forNumber(cmdByte) == null) {
            Log.e(" -- Socket -- ", "不支持的cmd命令");
        }
        int bodyLen = buffer.getInt();
        int readableLength = buffer.limit() - buffer.position();
        LogUtils.i(" -- Socket -- ", "bodyLen = " + bodyLen);
        LogUtils.i(" -- Socket -- ", "readableLength = " + readableLength);

        return bodyLen;
    }

    private void getLength(byte[] header) {
//        boolean isCompress = true;
//        boolean is4ByteLength = true;
//        boolean isEncrypt = true;
//        boolean isHasSynSeq = packet.getSynSeq() > 0;
//        //协议版本号
//        byte version = Protocol.VERSION;
//
//        //协议标志位mask
//        byte maskByte = ImPacket.encodeEncrypt(version, isEncrypt);
//        maskByte = ImPacket.encodeCompress(maskByte, isCompress);
//        maskByte = ImPacket.encodeHasSynSeq(maskByte, isHasSynSeq);
//        maskByte = ImPacket.encode4ByteLength(maskByte, is4ByteLength);
    }
}
